package com.qqtech.qquan.circle.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleInviteStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleBase;
import com.qqtech.qquan.circle.model.CircleInvite;
import com.qqtech.qquan.circle.model.CircleInviteQuery;
import com.qqtech.qquan.circle.model.CircleInviteVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleInviteService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/circle/invite")
public class CircleInviteController extends BaseWebController<CircleInvite, CircleInviteQuery> {
	@Resource
	private CircleInviteService circleInviteService;
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private CircleBaseService circleBaseService;
	
	@Override
	protected BaseService<CircleInvite> getBaseService() {
		return circleInviteService;
	}
	
	/**
	 * 添加圈子邀请
	 * @param circleInvite {beInviterId: xxx,circleId:XXX,msg:XXX}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody CircleInvite circleInvite) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		circleInvite.setInviterId(curUserId);
		circleInviteService.addCircleInvite(circleInvite);
		return result;
	}
	
	
	/**
	 * 一次邀请多个好友加入圈子
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/createBath")
	@ResponseBody
	public Result createBath(@RequestBody Map<String, String> map) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id(圈子创建人)
		String beInviterIdsString = map.get("beInviterIds").toString();
		String circleId = map.get("circleId").toString();
		String msg = map.get("msg").toString();
		String[] beInviterIdsArray = beInviterIdsString.split(";");
		
		for(String bId : beInviterIdsArray){
			CircleInvite circleInvite = new CircleInvite();
			circleInvite.setInviterId(curUserId);
			circleInvite.setBeInviterId(Integer.parseInt(bId));
			circleInvite.setCircleId(Integer.parseInt(circleId));
			circleInvite.setMsg(msg);
			circleInviteService.addCircleInvite(circleInvite);
		}
		return result;
	}
	
	/**
	 * 接受圈子邀请
	 * @param circleInvite {id:xxx}
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/accept")
	@ResponseBody
	public  Result accept(@RequestBody CircleInvite circleInvite) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer inviteId = circleInvite.getId();
		if(inviteId==null){
			result.addErr("邀请id为空！");
			return result;
		}
		
		circleInvite = circleInviteService.getById(inviteId);
		if(circleInvite==null){
			result.addErr("找不到对应的圈子邀请记录！");
			return result;
		}
		
		if(!curUserId.equals(circleInvite.getBeInviterId())){
			result.addErr("权限不足！");
			return result;
		}
		circleInviteService.accept(circleInvite);
		return result;
	}
	/**
	 * 忽略圈子邀请
	 * @param circleInvite {id:xxx}
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/ignore")
	@ResponseBody
	public  Result ignore(@RequestBody CircleInvite circleInvite) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer inviteId = circleInvite.getId();
		if(inviteId==null){
			result.addErr("邀请id为空！");
			return result;
		}
		
		circleInvite = circleInviteService.getById(inviteId);
		if(!curUserId.equals(circleInvite.getBeInviterId())){
			result.addErr("权限不足！");
			return result;
		}
		
		circleInviteService.ignore(circleInvite);
		return result;
	}
	
	/**
	 * 删除圈子邀请
	 * @param circleInvite {id:xxx}
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/remove")
	@ResponseBody
	public  Result remove(@RequestBody CircleInvite circleInvite) throws BizzException{
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		Integer inviteId = circleInvite.getId();
		if(inviteId==null){
			result.addErr("邀请id为空！");
			return result;
		}
		
		circleInvite = circleInviteService.getById(inviteId);
		if(!curUserId.equals(circleInvite.getBeInviterId())){
			result.addErr("权限不足！");
			return result;
		}
		
		circleInviteService.remove(circleInvite);
		return result;
	}
	
	
	/**
	 * 我的圈子邀请列表（别人邀请我的）
	 * @param friendApplyQ
	 * @return
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/myCircleInviteList")
	@ResponseBody
	public  Result myCircleInviteList(@RequestBody CircleInviteQuery circleInviteQ){
		Result result = new Result();
		UserBaseVo userVo= null;//用于取用户呢称、头像
		Integer curUserId = super.getUserId();//当前用户id
		circleInviteQ.setBeInviterId(curUserId);//我是被邀请人
		circleInviteQ.setStatus(CircleInviteStatusDictEnum.WAITING.getCode());
		CircleBase circle = null;
		Pageable pageKit = new PageKit(circleInviteQ.getPage(), new PageOrder(Direction.DESC, "newAt"));
		List<CircleInviteVo> inviteList = circleInviteService.queryList(circleInviteQ, pageKit);
			if(inviteList!=null && inviteList.size()>0){
				for(CircleInviteVo inviteVo:inviteList){
					userVo  = userBaseService.getById(inviteVo.getInviterId());
					inviteVo.setInviterImg(userVo.getLogo());
					inviteVo.setInviterName(userVo.getNick());
					circle = circleBaseService.getById(inviteVo.getCircleId());
					if(circle!=null){
						inviteVo.setCircleName(circle.getName());
					}
				}
			}
		
		result.put("inviteList", inviteList);
		return result;
	}
}
